<?php
/**
 * @name 数据检索类
 *
 * LICENSE
 *
 * This source file is subject to the BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.phpxe.com/license/
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to Chaodong.Sheng <scd113@gmail.com> so we can send you a copy immediately.
 *
 * @category   Db of Scd Framework
 * @package    Scd
 * @copyright  Copyright (c) 2007 PHPXe Web Studio <http://www.phpxe.com>
 * @license    http://framework.phpxe.com/license/
 * @version    $Id: Select.php Thu Sep 20 20:50:42 CST 2007 Davy $
 */
include_once("Scd/Db/Exception.php");
include_once("Scd/Db.php");

//	 * @name RecordCount
//	 * @abstract 得到数据记录
//	 * @param $ADODB_CACHE_DIR = 'ADOdbcache';
//
//	$rs = &$db->CacheExecute(600, $sql_2);
//	print $rs->RecordCount();
//	echo "<hr>";
//	 * @name FieldCount()
//	 * @abstract 返回字段数量
//	print $rs->Fieldcount();

/**
 * @name 数据库select
 * @todo  还没有加入cache
 * @version 1.0
 */

class Scd_Db_Select extends Scd_Db
{
	var $_dbo	=null;
	var $_fields = null;
	var $_table = null;
	var $_where = null;
	var $_order = null;
	var $_limit = null;

	function Scd_Db_Select( $config )
	{
		parent::Scd_Db( &$config );
	}

	/**
	 * set select fields
	 */
	function setFields($fields = '*')
	{
		return $this->_fields;
	}

	/**
	 * set select table
	 */
	function setTable($table)
	{
		return $this->_table = $table;
	}

	/**
	 * set select where
	 */
	function setWhere($where)
	{
		$where = $this->_prepareConditions($where);
		if ($where) {
			$this->_where = ' WHERE ' . $where;
		} else {
			$this->_where = '';
		}
		return $this->_where;
	}

	/**
	 * set select order by
	 */
	function setOrder($order)
	{
		if ($order != '') {
			$this->_order = ' ORDER BY ' . $order;
		} else {
			$this->_order = '';
		}
		return $this->_order;
	}

	/**
	 * set select limit
	 */
	function setLimit($limit)
	{
		// 处理 $limit
		if (is_array($limit)) {
			list($length, $offset) = $limit;
		} else {
			$length = $limit;
			$offset = null;
		}
		if ($length !== null || $offset !== null) {
			$this->_limit = " limit ".$length.", ".$offset;
		} else {
			$this->_limit = '';
		}
		return $this->_limit;
	}

	function setSql($tbl, $where, $order, $limit)
	{
		$this->setTable($tbl);
		$this->setWhere($where);
		$this->setOrder($order);
		$this->setLimit($limit);
	}

	/**
     * 查询所有符合条件的记录及相关数据
     *
     * @param mixed $conditions 查询条件
     * @param string $sort
     * @param mixed $limit
     * @param string $fields
     *
     * @return array
     */
	function findAll()
	{
		// 查询当前表
		//$sql = 'SELECT ' . $fields . ' FROM ' . $table;
		$sql = 'SELECT ' . $this->_fields . ' FROM ' . $this->_table . $this->_where . $this->_order .$this->_limit;
		return $this->query($sql);
	}

	/**
	 * @name GetOne
	 * @abstract 得到一个数据值
	 * @example $db->GetOne("select `subject` from design where pid='1'");//Return subject (string)
	 */
	function getOne($sql)
	{
		$this->_sql = $sql;
		return $this->_dbo->GetOne($sql);
	}

	/**
	 * @name GetRow
	 * @abstract 得到一行数据值；
	 * @example $db->GetRow("select * from design where pid='1'");
	 */
	function getRow($sql)
	{
		$this->_sql = $sql;
		$arr = $this->_dbo->GetRow($sql);
		if (!is_array($arr)) {
			new Scd_Db_Exception("the function : 'getAll' of 'Scd_Db_Select' Error \n May be your Sql was wrong!\n The SQL: ".$sql);
		}/*
		if (key_exists('time', $arr)) {
			$arr['time'] = $this->_dbo->UserDate($arr['time']);
		}*/
		return $arr;
	}

	/**
	 * @name GetAll
	 * @abstract 得到所有sql的数组
	 * @example $db->GetAll("select * from design ");
	 */
	function getAll($sql)
	{
		$this->_sql = $sql;
		$arr = $this->_dbo->GetAll($sql);
		if (!is_array($arr)) {
			new Scd_Db_Exception("the function : 'getAll' of 'Scd_Db_Select' Error \n May be your Sql was wrong!\n The SQL: ".addslashes($sql));
		}
		return $arr;
	}

	/**
	 * @name GetAssoc
	 * @abstract 得到一组对应关系；
	 * @example $db->GetAssoc("select `pid`, `sort`  from design");//Return array('pid'=>'sort');
	 */
	function getAssoc($sql)
	{
		$this->_sql = $sql;
		return $this->_dbo->GetAssoc($sql);
	}

}
?>